/**
 * 字符串去除所有空格
 * @param str 需要去除空格的字符串
 * @param {'both' | 'left' | 'right' | 'all'} type  both: 是去掉前后，all是去除所有字符串内的空格 如果不对，就返回字符串本身
 * @returns {*} 返回结果
 */
export function trim(str, type = 'both') {
  if (type === 'both') {
    return str.replace(/^\s+|\s+$/g, '')
  } else if (type === 'left') {
    return str.replace(/^\s*/, '')
  } else if (type === 'right') {
    return str.replace(/(\s*$)/g, '')
  } else if (type === 'all') {
    return str.replace(/\s+/g, '')
  } else {
    return str
  }
}

/**
 * 替换字符
 * @param text
 * @param findStr
 * @param replaceFor
 */
export function replaceAll(text, findStr, replaceFor) {
  return text.replace(new RegExp(`${findStr}`, 'gm'), replaceFor)
}

/**
 * 加密手机号显示
 * @example 13888888888 -> 138****8888
 * @param mobile 手机号码
 * @param placeholder 占位符 默认 ****
 */
export function encryptMobile(mobile, placeholder = '****') {
  // `${text.substr(0, 3)}****${text.substr(7)}`
  return mobile.length === 11 ? mobile.replace(/^(\d{3})\d{4}(\d{4})$/, `$1${placeholder}$2`) : mobile
}

/**
 * 默认的姓名脱敏规则
 * @param name
 * @param placeholder 占位符 默认 *
 * @returns {string}
 * @example 陈某某 -> 陈*某
 */
export function encryptName(name, placeholder = '*') {
  let value = ''
  if (name.length === 2) {
    value = name.substr(0, 1) + placeholder
  } else if (name.length > 2) {
    let char = ''
    for (let i = 0, len = name.length - 2; i < len; i++) {
      char += placeholder
    }
    value = name.substr(0, 1) + char + name.substr(-1, 1)
  } else {
    value = name
  }
  return value
}

/**
 * 默认身份证号脱敏规则
 * @param cid
 * @param placeholder 占位符 默认 ****
 * @returns {*}
 * @example 510202198809091212 -> 陈*某
 */
export function encryptCid(cid, placeholder = '********') {
  return cid.length === 18 ? cid.replace(/^(\d{6})\d{8}(\d{4})$/, `$1${placeholder}$2`) : cid
}

/**
 * 补充指定长度的占位符
 * @param num 需要填充的数字
 * @param params{maxLength, fillString, type} maxLength 最大长度 ，fillString 最大长度，type: left|right
 */
export function pad(num, { maxLength, fillString, type } = {
  maxLength: 2,
  fillString: '0',
  type: 'left',
}) {
  let str = `${num}`
  // string 现在有
  // padStart(maxLength: number, fillString?: string): string;
  // padEnd(maxLength: number, fillString?: string): string;
  while (str.length < maxLength) {
    if (type === 'left') {
      str = `${fillString}${str}`
    } else if (type === 'right') {
      str = `${str}${fillString}`
    } else {
      str = `${fillString}${str}`
    }
  }
  return str
}

/**
 * 获取数字
 * @param value
 * @returns {string|number}
 */
export function extraNumber(value) {
  value = String(value)
  const matchArr = value.match(/\d+(.\d+)?/g)
  if (matchArr) {
    return Number(matchArr[0])
  }
  return 0
}

/**
 * 把连续出现多次的字母字符串进行压缩。aaabbbbcccccd=>3a4b5cd
 * @param {string} value 需要压缩的字符串
 * @param {boolean} ignoreCase 是否忽略大小写
 */
function compressLetter(value, ignoreCase = false) {
  const pattern = new RegExp('([a-zA-Z])\\1+', ignoreCase ? 'ig' : 'g')

  return value.replace(pattern, (matchStr, group_1) => {
    return matchStr.length + group_1
  })
}

/**
 * @desc 唯一标识，随机数
 * @param length 随机数位数 默认6
 */
function unique(length = 6) {
  length = length || 6
  let rnd = ''
  for (let i = 0; i < length; i++)
    rnd += Math.floor(Math.random() * 10)
  return `ftui_${new Date().getTime()}${rnd}`
}

export default {
  extraNumber,
  encryptName,
  encryptMobile,
  encryptCid,
  trim,
  pad,
  replaceAll,
  compressLetter,
  unique,
}
